
#include "pub.h"
#include <QCryptographicHash>
#include "IDL_TypeSupport.h"

pub::pub(const int &id)
{
    participant =
            DomainParticipantFactory::get_instance()->create_participant(
                    id, PARTICIPANT_QOS_DEFAULT/* participant默认QoS */,
                    NULL /* listener */, STATUS_MASK_NONE);
    if (participant == NULL) {
        fprintf(stderr, "create_participant error\n");
        publisher_shutdown(participant);
        return ;
    }

    publisher = participant->create_publisher(
            PUBLISHER_QOS_DEFAULT /* 默认QoS */,
            NULL /* listener */, STATUS_MASK_NONE);
    if (publisher == NULL) {
        fprintf(stderr, "create_publisher error\n");
        publisher_shutdown(participant);
        return ;
    }

    type_name = UserDataTypeTypeSupport::get_type_name();
    retcode = UserDataTypeTypeSupport::register_type(
            participant, type_name);
    if (retcode != RETCODE_OK) {
        fprintf(stderr, "register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return ;
    }

    topic = participant->create_topic(
            "Example UserDataType"/* 主题名 */,
            type_name /* 类型名 */, TOPIC_QOS_DEFAULT/* 默认QoS */,
            NULL /* listener */, STATUS_MASK_NONE);
    if (topic == NULL) {
        fprintf(stderr, "create_topic error\n");
        publisher_shutdown(participant);
        return ;
    }

    writer = publisher->create_datawriter(
            topic , DATAWRITER_QOS_DEFAULT,
            NULL /* listener */, STATUS_MASK_NONE);
    if (writer == NULL) {
        fprintf(stderr, "create_datawriter error\n");
        publisher_shutdown(participant);
        return ;
    }
    UserDataType_writer = UserDataTypeDataWriter::narrow(writer);
    if (UserDataType_writer == NULL) {
        fprintf(stderr, "DataWriter narrow error\n");
        publisher_shutdown(participant);
        return ;
    }

}

int pub::publisher_shutdown(DomainParticipant *participant) {
   ReturnCode_t retcode;
   int status = 0;
   if (participant != NULL) {
       retcode = participant->delete_contained_entities();
       if (retcode != RETCODE_OK) {
           fprintf(stderr, "delete_contained_entities error %d\n", retcode);
           status = -1;
       }
       retcode =
               DomainParticipantFactory::get_instance()->delete_participant(participant);
       if (retcode != RETCODE_OK) {
           fprintf(stderr, "delete_participant error %d\n",
                   retcode);
           status = -1;
       }
   }
   return status;
}

pub::~pub()
{
    /* 8. 删除数据样本 */
    retcode = UserDataTypeTypeSupport::delete_data(instance);
    if (retcode != RETCODE_OK) {
        fprintf(stderr, "UserDataTypeTypeSupport::delete_data error %d\n", retcode);
    }
}


bool pub::send_message(const std::string &message) {
    if (!instance) {
        instance = UserDataTypeTypeSupport::create_data();
    }
    if (instance) {
        strcpy(instance->message, message.c_str());
        QByteArray str=QCryptographicHash::hash(message.c_str(),QCryptographicHash::Md5).toHex();
        instance->md5 = str.data();
        retcode = UserDataType_writer->write(*instance, instance_handle);
        return retcode == RETCODE_OK;
    }
    return false;
}
